package com.itlong.cloud.operate.service.impl;


import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.operate.OperateWithdrawPageDTO;
import com.itlong.cloud.POJO.VO.operate.OperateFinaceInfoVO;
import com.itlong.cloud.POJO.VO.operate.OperateWithdrawVO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentVO;
import com.itlong.cloud.commons.chinapay.service.IChinaPayTransferAccountService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.operate.dao.IOperateWithdrawDao;
import com.itlong.cloud.operate.service.IOperateWithdrawService;
import com.itlong.cloud.property.dao.IPropertyWithdrawDao;
import com.itlong.cloud.property.service.IPropertyWithdrawService;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      运营-财务管理-财务提现审接口实现类
 * </desc>
 *
 * @createDate 2018/09/20
 */
@Service
public class OperateWithdrawServiceImpl implements IOperateWithdrawService {

    @Autowired
    IOperateWithdrawDao iOperateWithdrawDao;

    @Autowired
    private MessageSender messageSender;

    @Autowired
    IChinaPayTransferAccountService iChinaPayTransferAccountService;

    @Autowired
    IPropertyWithdrawDao iPropertyWithdrawDao;

    @Autowired
    IPropertyWithdrawService iPropertyWithdrawService;

    /**
     * <desc>
     *      财务提现审核信息分页查询
     * <desc/>
     *
     * @param operateWithdrawPageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateWithdrawVO> getWithdrawInfoPage(OperateWithdrawPageDTO operateWithdrawPageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateWithdrawPageDTO);
        //分页类默认pageSize为20
        Page<OperateWithdrawVO> page=new Page<>();
        page.getPage().setPageSize(operateWithdrawPageDTO.getPageSize());
        page.getPage().setCurrentPage(operateWithdrawPageDTO.getCurrentPage());
        params.put("projectId",operateWithdrawPageDTO.getProjectId());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        //填充返回数据集
        List<OperateWithdrawVO> list = iOperateWithdrawDao.getWithdrawInfoPage(params);
        List<OperateWithdrawVO> listAfter = new ArrayList<>();
        //替换银行卡号 处理只显示最后4位
        for (OperateWithdrawVO operateWithdrawVO:list) {
            if (operateWithdrawVO.getBankCardNo() != null && operateWithdrawVO.getBankCardNo().length() >= 4) {
                String replaceStr = "";
                for (int i=0 ;i<operateWithdrawVO.getBankCardNo().length()-4;i++) {
                    replaceStr+="*";
                }
                operateWithdrawVO.setBankCardNo(replaceStr+operateWithdrawVO.getBankCardNo().substring(
                        operateWithdrawVO.getBankCardNo().length()-4,operateWithdrawVO.getBankCardNo().length()));
            }
            listAfter.add(operateWithdrawVO);
        }

        page.setRows(listAfter, iOperateWithdrawDao.getWithdrawInfoPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      审批拒绝
     * <desc/>
     *
     * @param cashIds 提现记录ID,逗号分隔
     * @param myUserName 登陆用户编号
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer refuse(String cashIds,String myUserName) throws Exception {
        //回退金额
        //更新项目可提现金额,实时金额
        List<OperateWithdrawVO> list = iOperateWithdrawDao.getWithdrawInfoForExcle(cashIds.split(","));
        for (OperateWithdrawVO operateWithdrawVO:list) {
            if (!"4".equals(operateWithdrawVO.getTradeStatus())) {
                //更新项目可提现金额,实时金额
                Map<String,Object> map=new HashMap<>();
                map.put("arithmeticFlag", "0");//运算: 0加,1减
                map.put("amount", operateWithdrawVO.getAmount());//金额
                map.put("projectId", operateWithdrawVO.getProjectId());//项目编号
                map.put("updateNameFlag","");//0可提现余额,1实时余额   为空都更新

                messageSender.send(RabbitMQConstant.RABBITMQ_PROJECT_AMOUNT_MANAGER_QUEUE, JSON.toJSONString(map),
                        RabbitMQConstant.RABBITMQ_PAY_EXCHANGE,
                        RabbitMQConstant.RABBITMQ_PROJECT_AMOUNT_ROUTING_KEY);
            }
        }
        return iOperateWithdrawDao.refuse(cashIds.split(","),myUserName);
    }

    /**
     * <desc>
     *      修改备注
     * <desc/>
     *
     * @param cashId 提现记录ID
     * @param remark 备注
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateRemark(String cashId, String remark) throws Exception {
        return iOperateWithdrawDao.updateRemark(cashId,remark);
    }

    /**
     * <desc>
     *      修改状态
     * <desc/>
     *
     * @param cashId 提现记录ID
     * @param tradeStatus 3提现成功 4提现失败
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer updateTradeStatus(String cashId, String tradeStatus) throws Exception {
        return iOperateWithdrawDao.updateTradeStatus(cashId,tradeStatus);
    }

    /**
     * <desc>
     *      系统财务汇总
     * <desc/>
     *
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateFinaceInfoVO getFinaceInfo() throws Exception {
        OperateFinaceInfoVO operateFinaceInfoVO = new OperateFinaceInfoVO();
        String amount = iChinaPayTransferAccountService.getAccountBalance();//备付金金额
        if (amount == null || "".equals(amount)) {
            amount = "0.00";
        }
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
        amount = String.valueOf(Double.valueOf(amount)/100);
        String unwithdrawAll = iOperateWithdrawDao.getUnwithdrawAll()==null?"0.00":iOperateWithdrawDao.getUnwithdrawAll();
        String withdrawAll = iOperateWithdrawDao.getWithdrawAll()==null?"0.00":iOperateWithdrawDao.getWithdrawAll();//物业已提现(提现中)
        String wechatAccountAll = iOperateWithdrawDao.getWechatAccountAll()==null?"0.00":iOperateWithdrawDao.getWechatAccountAll();//微信金额

        String tradeAmount = iOperateWithdrawDao.getPropertyValueAccount();
        if (tradeAmount == null || "".equals(tradeAmount)) {
            tradeAmount = "0.00";
        }
        //微信商户号+备付金-增值费用
        String thirdPayAll = new BigDecimal(wechatAccountAll).add(new BigDecimal(amount)).subtract(new BigDecimal(tradeAmount)).toString();

        //处理保留2位
        operateFinaceInfoVO.setThirdPayAll(decimalFormat.format(new BigDecimal(thirdPayAll)));
        operateFinaceInfoVO.setUnwithdrawAll(unwithdrawAll);//物业未提现
        operateFinaceInfoVO.setWithdrawAll(withdrawAll);//物业已提现(提现中)
        return operateFinaceInfoVO;
    }

    /**
     * <desc>
     *      根据条件查询财务提现审核信息
     * <desc/>
     *
     * @param operateWithdrawPageDTO 查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-09-20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateWithdrawVO> getWithdrawInfoForExcle(OperateWithdrawPageDTO operateWithdrawPageDTO) throws Exception {
        //固定导出最多2000
        operateWithdrawPageDTO.setCurrentPage("1");
        operateWithdrawPageDTO.setPageSize("2000");
        //分页对象
        Page<PropertyPaymentVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateWithdrawPageDTO, page);
        return iOperateWithdrawDao.getWithdrawInfoPage(params);
    }

    /**
     * <desc>
     *      财务提现审核明细查询
     * <desc/>
     *
     * @param cashId 提现记录ID
     * @return
     * @author Qiang.S
     * @createDate 2018-09-27
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateWithdrawVO getWithdrawDetail(String cashId) throws Exception {
        return iOperateWithdrawDao.getWithdrawDetail(cashId);
    }

}
